package com.hencoder.hencoderpracticedraw1.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class Practice11PieChartView extends View {
    private int mWidth;
    private int mHeight;
    private int radius=100;
    private Paint paint;
    private List<PercentModel> datas;
    private float totalNum =0;
    private int centerX,centerY;
    private int textSize=12;
//    扇形中心延长线
    private int lineExtensionLength=5;
//    最大宽度
    private int maxWidth=5;
    private float offsetX,offsetY;
    public Practice11PieChartView(Context context) {
        super(context);
        init();
    }


    public Practice11PieChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Practice11PieChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        datas=new ArrayList<>();
        paint=new Paint();
        paint.setStyle(Paint.Style.FILL);
        radius=dip2px(radius);
        lineExtensionLength=dip2px(lineExtensionLength);
        addDatas(new PercentModel(Color.parseColor("#ef9a9a"),10,"幼儿园",false));
        addDatas(new PercentModel(Color.parseColor("#e1bee7"),20,"小学",false));
        addDatas(new PercentModel(Color.parseColor("#9fa8da"),30,"初中",true));
        addDatas(new PercentModel(Color.parseColor("#26a69a"),30,"高中",false));
        addDatas(new PercentModel(Color.parseColor("#fbc02d"),10,"大学",false));
        addDatas(new PercentModel(Color.parseColor("#fb602d"),10,"硕士",false));
//        countTotalCount();
    }

    public void addDatas(PercentModel percentModel) {
        datas.add(percentModel);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mHeight=getMeasuredHeight();
        mWidth=getMeasuredWidth();
        centerX=mWidth/2;
        centerY=mHeight/2;

    }
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawArc(canvas);
    }


    private void drawLineAndText(Canvas canvas, int start, float angles, String text, float progress,boolean isneedout) {
        int length=radius+dip2px(lineExtensionLength);
        paint.setStrokeWidth(5);
        float stopX, stopY;
        canvas.save();
        canvas.translate(centerX,centerY);
        stopX = (float) (length * Math.cos((2 * start + angles) / 2 * Math.PI / 180));
        stopY = (float) (length * Math.sin((2 * start + angles) / 2 * Math.PI / 180));
        if (isneedout){
            offsetX=(float) (dip2px(20) * Math.cos((2 * start + angles) / 2 * Math.PI / 180));
            offsetY=(float) (dip2px(20) * Math.sin((2 * start + angles) / 2 * Math.PI / 180));
        }
        canvas.drawLine((float) ((radius-20) * Math.cos((2 * start + angles) / 2 * Math.PI / 180)),
                (float) ((radius-20) * Math.sin((2 * start + angles) / 2 * Math.PI / 180)),
                stopX, stopY, paint);
        //画横线
        int dx;//判断横线是画在左边还是右边
        int endX;
        if (stopX > 0) {
            endX = (centerX - getPaddingRight() - dip2px(10));
        } else {
            endX = (-centerX + getPaddingLeft() + dip2px(10));
        }
        //画横线
        canvas.drawLine(stopX, stopY,
                endX, stopY, paint
        );
        dx = (int) (endX - stopX);
        int middleX= (int) ((stopX+endX)/2);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(dip2px(textSize));
        canvas.drawText(text,middleX,stopY-dip2px(4),paint);

        canvas.drawText(progress+"%",middleX,stopY+dip2px(textSize)+dip2px(2),paint);
        canvas.restore();
    }

    /**
     * 画扇形部分
      */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void drawArc(Canvas canvas) {
        float startAngle=0;
        for (PercentModel model:datas) {
            paint.setColor(model.color);
            float progress=model.percent/totalNum;
            float sweepAngle=progress*360;
            if (!model.isout){
                canvas.drawArc(centerX-radius,centerY-radius,centerX+radius,centerY+radius,startAngle,sweepAngle,true,paint);
            }else {
//               //向上移动圆心
                canvas.save();
                offsetX=(float) (dip2px(maxWidth) * Math.cos((2 * startAngle + sweepAngle) / 2 * Math.PI / 180));
                offsetY=(float) (dip2px(maxWidth) * Math.sin((2 * startAngle + sweepAngle) / 2 * Math.PI / 180));
                canvas.translate(centerX+offsetX,centerY+offsetY);
                canvas.drawArc(-radius,-radius,radius,radius,startAngle,sweepAngle,true,paint);
                canvas.restore();

            }

            drawLineAndText(canvas, (int) startAngle,sweepAngle,model.title,Math.round(progress*100),model.isout);
            startAngle+=sweepAngle;
        }
    }
    /**
     * 计算总的数据
     */
    private void countTotalCount(){
        for (PercentModel model:datas) {
            totalNum +=model.percent;
        }
    }
    static class PercentModel{
        int color;
        float percent;
        String title;
        boolean isout;
        public PercentModel(int color, float percent, String title, boolean isout) {
            this.color = color;
            this.percent = percent;
            this.title = title;
            this.isout=isout;
        }
    }

    int dip2px(float dipValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        totalNum=0;
        countTotalCount();

    }
}
